package com.ae.shield.client.gui;

import com.ae.shield.ModLib;
import com.ae.shield.client.gui.util.GuiHelper;
import com.ae.shield.common.gui.container.AssemblyContainer;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nonnull;
import java.util.ArrayList;

public class AssemblyScreen extends ContainerScreen<AssemblyContainer> {
    private static final ResourceLocation GUI = new ResourceLocation(ModLib.MOD_ID, "textures/gui/assembly_block.png");
    private final GuiHelper.FluidBar fluidBar;

    public AssemblyScreen(AssemblyContainer container, PlayerInventory inventory, ITextComponent titleIn) {
        super(container, inventory, titleIn);
        guiLeft = 0;
        guiTop = 0;
        xSize = 176;
        ySize = 174;
        GuiHelper guiHelper = new GuiHelper(this, xSize, ySize);
        fluidBar = guiHelper.addFluidBar(container.getTank(), 0);
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(@Nonnull MatrixStack matrixStack, float partialTicks, int x, int y) {
        getMinecraft().textureManager.bindTexture(GUI);
        //背景
        RenderSystem.color4f(1f, 1f, 1f, 1f);
        getMinecraft().textureManager.bindTexture(GUI);
        blit(matrixStack, guiLeft, guiTop, 0, 0, 176, 198);
        //进度条
        int l = (int) Math.floor(container.getProgressionScaled() * 32);
        blit(matrixStack, guiLeft + 65, guiTop + 76, 176, 117, l, 2);
        //能量
        int i = (int) Math.floor(container.getEnergyScaled() * 58);
        blit(matrixStack, guiLeft + 165, guiTop + 88 - i, 176, 117 - i, 3, i);
        //流体
        int fluidScaled = (int) Math.floor(container.getFluidScaled() * 58);
        fluidBar.drawFluid(8, 88 - fluidScaled, 16, fluidScaled);
        getMinecraft().textureManager.bindTexture(GUI);
        blit(matrixStack, guiLeft + 8, guiTop + 29, 176, 0, 16, 58);
    }

    @Override
    public void render(@Nonnull MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        renderBackground(matrixStack);
        super.render(matrixStack, mouseX, mouseY, partialTicks);
        renderHoveredTooltip(matrixStack, mouseX, mouseY);
        drawEnergy(matrixStack, mouseX, mouseY);
        fluidBar.drawFluidTip(mouseX, mouseY, 8, 30, 16, 58);

    }

    @Override
    protected void drawGuiContainerForegroundLayer(@Nonnull MatrixStack matrixStack, int x, int y) {
        font.func_243248_b(matrixStack, playerInventory.getDisplayName(), (float) playerInventoryTitleX, (float) playerInventoryTitleY + 33, 4210752);
        font.func_243248_b(matrixStack, container.getDisplayName(), 8f, 4.5f, 4210752);
    }

//    TODO 将方法抽取到GuiHelper
    private void drawEnergy(@Nonnull MatrixStack matrixStack, int mouseX, int mouseY) {
        if (163 + guiLeft < mouseX && mouseX < 169 + guiLeft && 28 + guiTop < mouseY && mouseY < 89 + guiTop) {
            ArrayList<ITextComponent> list = new ArrayList<>();
            list.add(new TranslationTextComponent("screen.ae.shield.assembly.energy").append(new StringTextComponent(container.getEnergy() + " RF")));
            func_243308_b(matrixStack, list, mouseX, mouseY);
        }
    }
}
